package com.littleHan.brushQuestions.web.start.service.impl;

import com.littleHan.brushQuestions.exception.FailedToInsertException;
import com.littleHan.brushQuestions.utils.StringUtil;
import com.littleHan.brushQuestions.web.start.dao.GeneralUserDao;
import com.littleHan.brushQuestions.web.start.dao.RoleDao;
import com.littleHan.brushQuestions.web.start.dao.UserDao;
import com.littleHan.brushQuestions.web.start.dao.UserRoleDao;
import com.littleHan.brushQuestions.web.start.pojo.GeneralUser;
import com.littleHan.brushQuestions.web.start.pojo.User;
import com.littleHan.brushQuestions.web.start.pojo.UserRole;
import com.littleHan.brushQuestions.web.start.service.RegisterService;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class RegisterServiceImpl implements RegisterService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private GeneralUserDao generalUserDao;

    @Autowired
    private UserRoleDao userRoleDao;

    @Autowired
    private RoleDao roleDao;

    @Override
    @Transactional(rollbackFor = {FailedToInsertException.class})
    public void generalUserRegister(GeneralUser generalUser) throws FailedToInsertException{
        String id=StringUtil.generateUUID();
        generalUser.setId(id);
        String salt=RandomStringUtils.randomAscii(5);
        generalUser.setSalt(salt);
        String password=generalUser.getPassword();
        generalUser.setPassword(StringUtil.md5(password+salt));
        generalUser.setRegisterTime(StringUtil.getSysTime());
        generalUser.setViolation("1");
        generalUser.setScore("0");
        //普通用户表插入数据
        int result=generalUserDao.setGeneralUser(generalUser);
        User user=new User();
        String userId=StringUtil.generateUUID();
        user.setId(userId);
        user.setGeneralUserId(id);
        //用户表插入数据
        int result2=userDao.setUser(user);
        //用户权限表插入数据
        //获取普通用户角色的id
        String roleId=roleDao.getRoleByName("普通用户");
        UserRole userRole=new UserRole();
        userRole.setId(StringUtil.generateUUID());
        userRole.setUserId(userId);
        userRole.setRoleId(roleId);
        int result3=userRoleDao.setUserRole(userRole);
        if (result<1 && result2<1 && result3<1){
            throw new FailedToInsertException("注册失败");
        }
    }

    @Override
    public boolean checkUsername(String username) {
        int num=generalUserDao.selectByUsername(username);
        //被注册返回false
        if (num>0) {
            return false;
        }
        return true;
    }

    @Override
    public boolean checkNickName(String nickName) {
        int num=generalUserDao.selectByNickName(nickName);
        //被注册返回false
        if (num>0) {
            return false;
        }
        return true;
    }
}
